/**
 * 
 */
package com.swd2010.mobileSearch.network;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.swd2010.mobileSearch.data.PersonBean;

import android.util.Log;


/**
 * @author haui
 *
 */
public class PersonSearchWebservice {

	
	//Haui
	
	private static final String classNameForLog = "PersonSearchWebservice";
    


	
	
	public static List<PersonBean> get(String  searchTerm) {
						
	    ArrayList<PersonBean> resultList = new ArrayList<PersonBean>();
	    
	    ConnectInformation conInformation = new ConnectInformation();
	    
	    HttpURLConnection conn = null;
	    StringBuilder jsonResults = new StringBuilder();

	    
	    
	    try {
	    	
	        
	    	URL url = conInformation.getPersonSearchUrl(searchTerm);
	    	
	        conn = (HttpURLConnection) url.openConnection();
	        InputStreamReader in = new InputStreamReader(conn.getInputStream());
	        
	        
	        // Load the results into a StringBuilder
	        int read;
	        
	        char[] buff = new char[1024];
	        
	        while ((read = in.read(buff)) != -1) {
	            jsonResults.append(buff, 0, read);
	        }
	        
	    } catch (MalformedURLException e) {
	    	
	        Log.e(classNameForLog, "Error: ", e);
	        return resultList;
	        
	    } catch (IOException e) {
	    	
	        Log.e(classNameForLog, "Error: ", e);
	        return resultList;
	        
	    } finally {
	    	
	        if (conn != null) {
	            conn.disconnect();
	        }
	        
	    }

	    try {
	    	
	    	
	    	if ( jsonResults.toString().equalsIgnoreCase("NULL") )
	    		return resultList;
	    	
	    	
	        // Create a JSON object hierarchy from the results
	        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        	JSONArray predsJsonArray = null;
	        
        	
        	//There ist a bug in Jersey JSON implementation and therefore we have to struggle
        	//around here.
        	//http://stackoverflow.com/questions/6237470/myobject-return-json-object-instead-of-array-when-size-2        	
	        try{
	        	
	        	predsJsonArray = jsonObj.getJSONArray("personBean");
	        	
		        // Extract the Place descriptions from the results
		        //resultList = new ArrayList<String>(predsJsonArray.length());	        
		        for (int i = 0; i < predsJsonArray.length(); i++) {
		        	
		        	PersonBean pB = new PersonBean();
		        	
		            pB.setFullNameWithDegress( (predsJsonArray.getJSONObject(i).getString("fullNameWithDegress") ) );
		            pB.setAdditionalInformation( (predsJsonArray.getJSONObject(i).getString("additionalInformation") ) );
		            pB.seteMail( (predsJsonArray.getJSONObject(i).getString("eMail") ) );
		            pB.setAddress( (predsJsonArray.getJSONObject(i).getString("address") ) );
		            pB.setId( (predsJsonArray.getJSONObject(i).getString("id") ) );
		        	
		            resultList.add(pB);
		            
		        }	        	
	        	
	        	
	        }catch(Exception e){
	        	
	        	PersonBean pB = new PersonBean();
	        	
	        	jsonObj = jsonObj.getJSONObject("personBean");
	        	
	            pB.setFullNameWithDegress( ( jsonObj.getString("fullNameWithDegress") ) );
	            pB.setAdditionalInformation( ( jsonObj.getString("additionalInformation") ) );
	            pB.seteMail( ( jsonObj.getString("eMail") ) );
	            pB.setAddress( ( jsonObj.getString("address") ) );
	            pB.setId( ( jsonObj.getString("id") ) );
	        	
	            resultList.add(pB);	        	
	        	
	        }



	        
	    } catch (JSONException e) {
	    	
	    	
	        Log.e(classNameForLog, "Cannot process JSON results", e);
	        
	        
	    }
	    
	    return resultList;
	
	}	
	
	
}
